﻿var view =
{
    currentView: null,
    openViews: [],
    hideIndex: function(viewIndex)
    {
        this.hide(this.openViews[viewIndex]);
    },
    hide: function(view)
    {
        var viewFound = false;
        var foundAt = -1;
        for (var i = 0; i < this.openViews.length; i++) {
            if (this.openViews[i] == $(view)[0]) { foundAt = i; break; }
        }

        this.openViews.length = foundAt;

        if (foundAt > 0) this.currentView = this.openViews[foundAt - 1];
        if (this.openViews.length < 2) $("#backbutton").hide("slide", { direction: "right" }, 200);

        $(this.currentView).fadeIn(200);
        $(this.currentView).animate({ marginLeft: "0px" }, 800, "easeOutSine", function () { $(view).hide(); });
        this.updateBreadcrumbs();
        this.fireViewSetup(this.currentView);
    },
    show: function (view) {
        this.openViews.push($(view)[0]);
        $(view).fadeIn(200);
        if (this.openViews.length == 1) {
            this.currentView = $(view)[0];
        }
        else {
            $("#backbutton").show("slide", { direction: "right" }, 200);
            $(view).fadeIn(200);
            $(this.currentView).animate({ marginLeft: "-1000px" }, 800, "easeOutSine", function () { $(this.currentView).hide(); });
            this.currentView = $(view)[0];
        }
        this.updateBreadcrumbs();
        this.fireViewSetup(view);
    },
    fireViewSetup: function (view) {
        if (view != null && $(view).attr("data-viewsetup") != null && typeof window[$(view).attr("data-viewsetup") == 'function']) window[$(view).attr("data-viewsetup")](view);
    },
    updateBreadcrumbs: function () {
        var bcContent = this.getBreadcrumbs();
        var bcTarget = $(".navigation .breadcrumb");
        bcTarget.empty();
        for (var i = 0; i < bcContent.length; i++) {
            if (bcContent[i].view == this.currentView) bcTarget.append($("<li class='active'>" + bcContent[i].title + "</li>"));
            else bcTarget.append($("<li><a href='#' onclick='view.hideIndex(" + (i+1) + ");'>" + bcContent[i].title + "</a> <span class='divider'>/</span></li>"));
        }
    },
    getBreadcrumbs: function () {
        var titles = [];
        for (var i = 0; i < this.openViews.length; i++) {
            var title = $(this.openViews[i]).attr("data-navtitle");
            if (title == null) title = "Section";
            titles.push(
                {
                    view: this.openViews[i],
                    title: title
                });
        }
        return titles;
    },
};


function showDialog(dialog, callback) {
    $("#dialogSpace").show();
    $("#dialogBackCover").show().animate({ opacity: .8 }, 1000);
    $(dialog).fadeIn(500, function () { if (callback != null) callback(); });
}
function hideDialog() {
    $(".dialog").slideUp(200);
    $("#dialogBackCover").fadeOut();
    $("#dialogSpace").fadeOut();
}

function stopError(reason) {
    if (reason != null) $("#disconnectedDialog #stopReason").html("<i class='icon-warning-sign icon-red'></i> " + reason);
    showDialog($("#disconnectedDialog"));
}